/**
 * TagSpaces - universal file and folder organizer
 * Copyright (C) 2017-present TagSpaces GmbH
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License (version 3) as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

import { app, Menu, nativeImage, Tray } from 'electron';

let tray;

const icon1x =
  '';
const icon1xMac =
  '';
const icon2xBlack =
  '';
const icon2xWhite =
  '';

export default function buildTrayMenu(
  mainPageProps: any,
  i18n,
  isMacLike,
  globalShortcutsEnabled,
) {
  // const cKey = isMacLike ? '  -  ⌘' : '  -  Ctrl';
  // const sKey = isMacLike ? '⇧' : 'Shift';
  // const pKey = isMacLike ? ' ' : ' + ';

  function openNextFile() {
    mainPageProps.openNextFile();
  }

  function openPrevFile() {
    mainPageProps.openPrevFile();
  }

  function playResumePlayback() {
    mainPageProps.resumePlayback();
  }

  function quitApp() {
    app.quit();
  }

  const trayMenuTemplate = [
    {
      label: i18n.t('newWindow'),
      click: () => mainPageProps.createNewWindowInstance(),
    },
    {
      type: 'separator',
    },
    {
      label: i18n.t('showTagSpaces'),
      accelerator: globalShortcutsEnabled ? 'CmdOrCtrl+Shift+w' : undefined,
      click: mainPageProps.showTagSpaces,
    },
    {
      label: i18n.t('showSearch'),
      accelerator: globalShortcutsEnabled ? 'CmdOrCtrl+Shift+f' : undefined,
      click: mainPageProps.openSearch,
    },
    {
      type: 'separator',
    },
    {
      label: i18n.t('newFileNote'),
      accelerator: globalShortcutsEnabled ? 'CmdOrCtrl+Shift+n' : undefined,
      click: mainPageProps.toggleNewFileDialog,
    },
    {
      type: 'separator',
    },
    {
      label: i18n.t('openNextFileTooltip'),
      accelerator: globalShortcutsEnabled ? 'CmdOrCtrl+Shift+d' : undefined,
      click: openNextFile,
    },
    {
      label: i18n.t('openPrevFileTooltip'),
      accelerator: globalShortcutsEnabled ? 'CmdOrCtrl+Shift+a' : undefined,
      click: openPrevFile,
    },
    {
      type: 'separator',
    },
    {
      label: i18n.t('pauseResumePlayback'),
      accelerator: globalShortcutsEnabled ? 'CmdOrCtrl+Shift+p' : undefined,
      click: playResumePlayback,
    },
    {
      type: 'separator',
    },
    {
      label: i18n.t('quitTagSpaces'),
      accelerator: 'CmdOrCtrl+q',
      click: quitApp,
    },
  ];

  if (!tray) {
    let icon;
    if (isMacLike) {
      icon = nativeImage.createFromDataURL(icon1xMac);
      icon.addRepresentation({
        scaleFactor: 2.0,
        dataURL: icon2xBlack,
      });
      icon.setTemplateImage(true);
    } else {
      icon = nativeImage.createFromDataURL(icon1x);
    }
    tray = new Tray(icon);
  }

  // @ts-ignore
  const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
  tray?.setToolTip('TagSpaces');
  tray?.setContextMenu(contextMenu);
}
